#-*- encoding: utf-8 -*-
import win32com.client 
from Misc.Utils import *

from DataAccess.QueryBlp import *

from datetime import date,timedelta

from ReportTool.HldReport import *
from ReportTool.PnLReport import *
from ReportTool.NAVReport import *
from ReportTool.MktValReport import *
from ReportTool.TradeReport import *
from ReportTool.RealizedPnL import *
from ReportTool.OnshoreHldReport import *

from Config.ReportConfig import *

def Citi_Daily_Position(ref_date):

	hld = query_hld(ref_date, portf_list=['SC028'])
	
	tplt_file = TPLT_PATH + 'Daily Position for Citi.xls'
	xApp = win32com.client.Dispatch("Excel.Application")
	xBook = xApp.Workbooks.Open(tplt_file)
	xSht = xBook.Worksheets('Report')
	
	row = 2
	for elem in hld:
		if elem[2] == 'CASH' or elem[2] == 'REPO':
			continue
		elif elem[2] == 'PNOTE':
			blp_ticker = ch_ticker_to_blp_ticker(elem[0])
		elif (elem[2] == 'STOCK' and elem[17] == 'HKD'):
			blp_ticker = hk_ticker_to_blp_ticker(elem[0])
		else:
			blp_ticker = elem[0]
			
		sec_name = query_blp_static([blp_ticker], ['NAME'])[blp_ticker]['NAME']
		
		xSht.Cells(row, 1).Value = ref_date
		xSht.Cells(row, 2).Value = 'SPRINGS CHINA OPPORTUNITIES MASTER FUND'
		xSht.Cells(row, 3).Value = elem[4]
		xSht.Cells(row, 4).Value = elem[5]
		xSht.Cells(row, 5).Value = elem[7]
		xSht.Cells(row, 6).Value = elem[9]
		xSht.Cells(row, 7).Value = elem[3]
		xSht.Cells(row, 8).Value = elem[17]
		xSht.Cells(row, 9).Value = elem[2]
		xSht.Cells(row, 10).Value = blp_ticker
		xSht.Cells(row, 11).Value = sec_name
		xSht.Cells(row, 12).Value = elem[10]
		
		row += 1
		
	rept_file = REPT_PATH + 'Position File_' + ref_date.strftime("%Y%m%d") + '.csv'
	xBook.SaveAs(rept_file, win32com.client.constants.xlCSV)
	xBook.Close()
	del xApp
	
	print 'info, citi daily position report completed at ', ref_date.isoformat()